﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using AngleSharp.Parser.Html;
using Cicada;
using Cicada.SQLite;

namespace HtmlAgilityPack
{
    class Program
    {
        public static SQLiteContext context = new SQLiteContext(new SQLiteConnectionFactory(@"Data Source=D:\ECRF\Asp.net Core\HtmlTest\HtmlAgilityPack\Chloe.db;"));
        static void Main(string[] args)
        {
            AddToHotMovieList();
            Console.ReadKey();
        }

        private static HtmlParser htmlParser = new HtmlParser();

        private static List<UserInfo> Infos = new List<UserInfo>();

        private static List<ImgInfo> imgList = new List<ImgInfo>();
        private static void AddToHotMovieList()
        {
            Task.Factory.StartNew(() =>
            {
                try
                {
                    bool IsNext = false;
                    var urlAddress = "http://date.jobbole.com";
                    do
                    {
                        IsNext = false;
                        var htmlDoc = HTTPHelper.GetHTMLByURL(urlAddress);
                        var dom = htmlParser.Parse(htmlDoc);
                        var lstDivInfo = dom.QuerySelectorAll("ul.list-posts li[class='media']");

                        if (lstDivInfo != null)
                        {
                            foreach (var divInfo in lstDivInfo.ToList())
                            {
                                var Info = divInfo.QuerySelector("div.media-body h3.p-tit");
                                var address = divInfo.QuerySelector("div.media-body p.p-meta");

                                if (address.QuerySelector("a") == null)
                                    continue;
                                UserInfo userInfo = new UserInfo();
                                userInfo.Id = divInfo.QuerySelector("a[data-post-id]").GetAttribute("data-post-id");
                                userInfo.Url = Info.QuerySelector("a").GetAttribute("href");
                                userInfo.Info = Info.QuerySelector("a").InnerHtml;
                                userInfo.address = address.QuerySelector("a").InnerHtml;
                                Infos.Add(userInfo);
                            }
                        }
                        var pagination = dom.QuerySelector("ul.pagination li[id='pagination-next-page']");
                        if (pagination == null || pagination.QuerySelector("a") == null)
                        {
                            IsNext = false;
                        }
                        else
                        {
                            IsNext = true;
                            Console.WriteLine("next:" + pagination.QuerySelector("a").GetAttribute("href"));
                            urlAddress = pagination.QuerySelector("a").GetAttribute("href");
                        }
                    } while (IsNext);
                    Console.WriteLine("获取信息完成:" + urlAddress);

                    foreach (var item in Infos)
                    {
                        var htmlImg = HTTPHelper.GetHTMLByURL(item.Url);
                        var Imgdom = htmlParser.Parse(htmlImg);
                        var ImgInfo = Imgdom.QuerySelector("div.p-single div.p-entry");

                        item.describe = ImgInfo.QuerySelector("p").OuterHtml;
                        Console.WriteLine(item.address + "_" + item.Id + ":" + item.describe);
                        ImgInfo.QuerySelectorAll("img").ToList().ForEach(img =>
                        {
                            string filename = FileDownSave(img.GetAttribute("src"), "C:\\test\\", item.address + "_" + item.Id);
                            imgList.Add(new ImgInfo()
                            {
                                Id = item.Id,
                                img = img.GetAttribute("src"),
                                localimg = filename
                            });
                        });
                    }

                    Console.WriteLine("下载图片完成:" + Infos.Count);
                    foreach (var item in Infos)
                    {
                        context.Insert<UserInfo>(item);
                    }
                    Console.WriteLine("写入UserInfo表完成");
                    foreach (var item in imgList)
                    {
                        context.Insert<ImgInfo>(item);
                    }
                    Console.WriteLine("写入数据库完成:" + imgList.Count);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("error:" + ex.Message);
                }
            });
        }

        private static HttpClient httpClient = null;
        /// <summary>
        /// 下载并保存
        /// </summary>
        /// <param name="url">网络路径</param>
        /// <param name="savePath">保存本地的文件夹</param>
        public static string FileDownSave(string url, string savePath, string Id)
        {
            try
            {
                string filename = string.Empty;
                if (!string.IsNullOrWhiteSpace(url))
                {
                    string[] strArry = url.Split('/');
                    filename = Id + "_" + strArry[strArry.Length - 1];
                    savePath = savePath + "/" + Id + "_" + strArry[strArry.Length - 1];
                }
                if (httpClient == null)
                {
                    httpClient = new HttpClient();
                }
                var t = httpClient.GetByteArrayAsync(url);
                t.Wait();
                Stream responseStream = new MemoryStream(t.Result);
                Stream stream = new FileStream(savePath, FileMode.Create);
                byte[] bArr = new byte[1024];
                int size = responseStream.Read(bArr, 0, bArr.Length);
                while (size > 0)
                {
                    stream.Write(bArr, 0, size);
                    size = responseStream.Read(bArr, 0, bArr.Length);
                }
                stream.Close();
                responseStream.Close();
                return filename;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("error:" + ex.Message);
                return string.Empty;
            }
        }
    }
}
